const tagsview = {
  state: {
    visitedviews: [], //存放所有浏览过的且不重复的路由数据
  },
  getters: {
    visitedviews: state => state.visitedviews
  },
  mutations: { //这
    ADD_VISITED_VIEW: (state, view) => { //打开新页签--添加路由数据的方法      if(state.visitedviews.some(v=>v.path==view.path))return
      let exist = state.visitedviews.some(v => v.path == view.path)
      if (exist) {
        return
      }
      const dashboard = {
        path: '/dashboard',
        name: 'dashboard',
        title: '首页',
        componentName: null
      }
      let path = view.path
      let componentNameParam = view.componentName
      if (!componentNameParam && view.meta && view.meta.componentName) componentNameParam = view.meta.componentName

      if (path === dashboard.path || path === '') {
        state.visitedviews.splice(0, 0, {
          name: view.name,
          path: view.path,
          title: view.title || view.meta.title || 'no-title',
          componentName: componentNameParam
        })
        return
      }

      let dashboardExist = state.visitedviews.some(v => v.path == dashboard.path)
      if (!dashboardExist) {
        state.visitedviews.splice(0, 0, {
          name: dashboard.name,
          path: dashboard.path,
          title: dashboard.title || 'no-title',
          componentName: dashboard.componentName
        })
      }

      state.visitedviews.splice(0, 0, {
        name: view.name,
        path: view.path,
        title: view.title || view.meta.title || 'no-title',
        componentName: componentNameParam
      })
    },
    DEL_VISITED_VIEW: (state, view) => { //关闭页签--删除路由数据的方法
      console.log('state.visitedviews.entries()', state.visitedviews.entries())
      console.log('state.visitedviews.view', view)
      for (let [i, v] of state.visitedviews.entries()) {
        if (v.path == view.path) {
          state.visitedviews.splice(i, 1)
          break
        }
      }
    },
    DEL_VISITED_VIEWS: (state, views) => {
      // 关闭页签--删除路由数据的方法
      let i = state.visitedviews.length
      // eslint-disable-next-line no-plusplus
      while (i--) {
        let v = state.visitedviews[i]
        if (views.some(t => t.path === v.path)) {
          state.visitedviews.splice(i, 1)
        }
      }
    },
    DEL_ALL_VISITED_VIEWS: state => {
      state.visitedviews = []
    },
  },
  actions: { //调用这里去触发mutations，如何调用？在组件内使用this.$store.dispatch('action中对应名字', 参数)
    addVisitedView({commit}, view) { //通过解构赋值得到commit方法
      commit('ADD_VISITED_VIEW', view) //去触发ADD_VISITED_VIEWS，并传入参数
    },
    delVisitedView({commit, state}, view) { //删除数组存放的路由之后，需要再去刷新路由，这是一个异步的过程，需要有回掉函数，所以使用并返回promise对象，也可以让组件在调用的时候接着使用.then的方法
      //commit('DEL_VISITED_VIEWS',view)
      return new Promise((resolve) => { //resolve方法：未来成功后回掉的方法
        commit('DEL_VISITED_VIEW', view)
        resolve([...state.visitedviews])
      })
    },
    delVisitedViews({commit, state}, views) {
      //commit('DEL_VISITED_VIEWS',view)
      return new Promise((resolve) => { //resolve方法：未来成功后回掉的方法
        commit('DEL_VISITED_VIEWS', views)
        resolve([...state.visitedviews])
      })
    },
    delAllVisitedViews({commit, state}) {
      return new Promise(resolve => {
        commit('DEL_ALL_VISITED_VIEWS')
        resolve([])
      })
    },

  }
}

export default tagsview
